﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;


public partial class Upload : System.Web.UI.Page
{
    private string uploadDirectory;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        lbl_uploadinfo.Text = "";
        uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "Uploads");
       


        if (txt_p_name.Text == "" || txt_p_equipment.Text == "" || txt_p_exposure.Text == "")
        {
            lbl_uploadinfo.Text = "Fill the required fields!" + "<br/>";
     
        }
        else
        {
            // Check that a file is actually being submitted.
            if (Uploader.PostedFile.FileName == "")
                lbl_uploadinfo.Text += "No file specified.";
            else       
                if (Uploader.PostedFile.ContentLength > 1000076)
                {
                    lbl_uploadinfo.Text = "The File size is too big! <br/> Should be less than 1MB";
                    return;
                }
                else
                {
                    // Check the extension.
                    string extension = Path.GetExtension(Uploader.PostedFile.FileName);
                    switch (extension.ToLower())
                    {
                        case ".jpg":
                            break;
                        default:
                            lbl_uploadinfo.Text += "This file type is not allowed.";
                            return;
                    }
                    // Using this code, the saved file will retain its original
                    // file name when it's placed on the server.



     // GO ZEMA BROJOT NA VEKJE UPLOADIRANI SLIKI-------------------------------------------------


                    SqlConnection myConnection = new SqlConnection();
                    myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
                    string sqlQuestion = "SELECT * FROM Photos";

                    SqlCommand komanda = new SqlCommand(sqlQuestion, myConnection);
                    SqlDataReader citac;

                    int broj_sliki = 0;

                    try
                    {
                        myConnection.Open();
                        citac = komanda.ExecuteReader();
                        while (citac.Read())
                        {
                            broj_sliki++;
                        }
                        lbl_uploadinfo.Text = broj_sliki.ToString();
                    }
                    catch (Exception err)
                    {
                        lbl_uploadinfo.Text = " ";
                        lbl_uploadinfo.Text += err.Message;
                        myConnection.Close();
                        return;
                    }
                    finally
                    {
                        myConnection.Close();
                    }



     // GO UPLOADIRA FAJLOT---------------------------------------------------------------------

                    byte[] imageSize = new byte[Uploader.PostedFile.ContentLength];

                    string serverFileName = "photo_" + Convert.ToString(++broj_sliki) + ".jpg";
                    string fullUploadPath = Path.Combine(uploadDirectory, serverFileName);

                    string imagestring = "~/Uploads/" + serverFileName.ToString();
                    try
                    {
                        Uploader.PostedFile.SaveAs(fullUploadPath);
                        lbl_uploadinfo.Text += "File " + serverFileName;
                        lbl_uploadinfo.Text += " uploaded successfully!";

                        //lbl_uploadinfo.Text += fullUploadPath;
                    }
                    catch (Exception err)
                    {
                        lbl_uploadinfo.Text = err.Message;
                        return;
                    }

                    //// GI VNESUVA PODATOCITE----------------------------------------------------------------------



                    string insertSql;
                    insertSql = "INSERT INTO Photos (username,image,points,votes,location_taken,name,equipment,exposure)";
                    insertSql += "VALUES(";
                    insertSql += "@username,@image,@points,@votes,@location_taken,@name,@equipment,@exposure)";

                    SqlCommand cmd = new SqlCommand(insertSql, myConnection);

                    //cmd.Parameters.AddWithValue("@ID", broj_sliki);
                    cmd.Parameters.AddWithValue("@username", Session["username"].ToString());
                    cmd.Parameters.AddWithValue("@name", txt_p_name.Text);
                    cmd.Parameters.AddWithValue("@points", 0);
                    cmd.Parameters.AddWithValue("@image", imagestring);
                    cmd.Parameters.AddWithValue("@equipment", txt_p_equipment.Text);
                    cmd.Parameters.AddWithValue("@location_taken", txt_p_location.Text);
                    cmd.Parameters.AddWithValue("@exposure", txt_p_exposure.Text);
                    cmd.Parameters.AddWithValue("@votes", 0);

                    int added = 0;
                    try
                    {
                        myConnection.Open();
                        added = cmd.ExecuteNonQuery();
                        lbl_uploadinfo.Text += added.ToString() + " records inserted.";
                    }
                    catch (Exception err)
                    {
                        lbl_uploadinfo.Text += "error adding a photo to database! ";
                        lbl_uploadinfo.Text += err.Message;
                    }
                    finally
                    {
                        myConnection.Close();
                    }
                    txt_p_name.Text = "";
                    txt_p_location.Text = "";
                    txt_p_exposure.Text = "";
                    txt_p_equipment.Text = "";

                }
        }

    }
}
